<?php
/**
 * Created by PhpStorm.
 * User: zouyanan
 * Date: 2019/5/11
 * Time: 下午10:35
 */

namespace app\userapi\controller;
use app\common\controller\Api;


/**
 * 动态接口
 */
class Trends extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    public function index(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $page = $this->request->request("page");
        //$limit = $this->request->request('limit',10);
        $list = db('yjy_house a')
            ->join('user b','a.user_id = b.id','LEFT')
            ->field('a.id,a.title,a.price,a.location_near,a.createtime,a.images')
            ->field('b.nickname,b.avatar')
            ->where("a.house_status",1)
            ->order("a.createtime desc")
            ->paginate(6);
        $items = $list->items();
        foreach($items as $key=>$val){
            $minute = floor((time() - $val['createtime'])/60);
            if($minute < 60){
                $items[$key]['minute'] = $minute.'分钟前';
            }else{
                $items[$key]['minute'] = date( "m-d H:i",$val['createtime']).'发布';
            }
            $items[$key]['imglist'] = array_slice(explode(',',$val['images']),0,3);
           // $items[$key]['avatar'] = $val['avatar'];
        }
        $this->success("返回成功",$items);
    }




    //添加热搜
    public function hotkeywordsAdd(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $keywords = $this->request->request("keywords");
        $is_exist = db("yjy_hot_search")->where(["name"=>$keywords])->value("id");
        if($is_exist){
            db("yjy_hot_search")->where("id",$is_exist)->setInc("time",1);
        }else{
            db("yjy_hot_search")->insert([
                "name" => $keywords
            ]);
        }
        $this->success("添加成功");
    }



    //热词
    public function hotkeywords(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
       $list =  db("yjy_hot_search")->field("id,name")->order("times desc")->order("id desc")->limit(4)->select();
        $this->success("返回成功",$list);
    }



















}